﻿using System.Security;

namespace AzManPermissions
{
  /// <summary>
  /// Description of one AzMan operation permission.
  /// </summary>
  internal class PermissionDescriptor
  {
    internal const string defaultObjectName = "DefaultCheck";
    internal const string defaultScopeName = "";
    internal const int defaultOperation = int.MinValue;
    internal string name;
    internal int operation = defaultOperation;
    internal string objectName = defaultObjectName;
    internal string scopeName = defaultScopeName;

    internal SecurityElement ToXml()
    {
      SecurityElement securityElement = new SecurityElement("Identity");
      if (name != null)
      {
        securityElement.AddAttribute("Name", SecurityElement.Escape(name));
      }
      if (operation != defaultOperation)
      {
        securityElement.AddAttribute("Operation", SecurityElement.Escape(operation.ToString()));
      }
      if (objectName != null)
      {
        securityElement.AddAttribute("ObjectName", SecurityElement.Escape(objectName));
      }
      if (scopeName != null)
      {
        securityElement.AddAttribute("ScopeName", SecurityElement.Escape(scopeName));
      }
      return securityElement;
    }

    internal void FromXml(SecurityElement e)
    {
      string text1 = e.Attribute("Name");
      if (text1 != null)
      {
        name = text1;
      }
      else
      {
        name = null;
      }
      string text2 = e.Attribute("Operation");
      if (text2 != null)
      {
        operation = int.Parse(text2);
      }
      else
        operation = defaultOperation;
      string text3 = e.Attribute("ObjectName");
      if (text3 != null)
      {
        objectName = text3;
      }
      else
        objectName = defaultObjectName;
      string text4 = e.Attribute("ScopeName");
      if (text4 != null)
      {
        scopeName = text4;
      }
      else
        scopeName = defaultScopeName;
    }
  }
}